


var play = play||{};
      
        play.doublePlay = false;	//关闭双人模式
        play.isJ 		=	false;
	play.isj 		=	false;	
        
        play.ykey1 = false;//记录升变前原key红
        play.ykey0 = false;//记录升变前原key黑
        play.my=1;	//玩家方默认红方
        play.isPlay 		=	true ;			//是否能走棋
play.init = function (){
	
	
        
       	play.map =	com.arr2Clone (com.initMap);		//初始化棋盘
	play.nowManKey	=	false;			//现在要操作的棋子
	play.pace =	[];		//记录每一步
	
	play.mans =	com.mans;     //棋子集合
      
	play.bylaw = 	com.bylaw;//棋子能走的着点
	play.show = 	com.show;
	play.showPane 		= 	com.showPane;
      	//play.isOffensive	=	true;			    //是否先手
	play.depth			=	play.depth || 3;    //搜索深度
	
	play.isFoul			=	false;	          //是否犯规长将
	com.pane.isShow		=	 false;			   //隐藏方块	

      
       
   Log("初始化+play.isj+");

	
	

	
	//初始化棋子
	for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
			if (key){
				com.mans[key].x=n;
				com.mans[key].y=i;                           
				com.mans[key].isShow = true;
                            

			}
		}
	}

	play.show();
	
	//绑定点击事件
	com.canvas.addEventListener("click",play.clickCanvas)
	//clearInterval(play.timer);
	//com.get("autoPlay").addEventListener("click", function(e) {
		//clearInterval(play.timer);
		//play.timer = setInterval("play.AIPlay()",1000);
	//	play.AIPlay()
	//})
	/*
	com.get("offensivePlay").addEventListener("click", function(e) {    //先手开始
		play.isOffensive=true;
		play.isPlay=true ;	
		com.get("chessRight").style.display = "none";
		play.init();
	})
	
	com.get("defensivePlay").addEventListener("click", function(e) {       //后手
		play.isOffensive=false;
		play.isPlay=true ;	
		com.get("chessRight").style.display = "none";
		play.init();
	})
	*/
	com.get("doublePlay").addEventListener("click", function(e) {
		play.doublePlay();
	})
	
	com.get("regretBn").addEventListener("click", function(e) {
		play.regret();
	})
	
	/*
	var initTime = new Date().getTime();
	for (var i=0; i<=100000; i++){
		
		var h=""
		var h=play.map.join();
		//for (var n in play.mans){
		//	if (play.mans[n].show) h+=play.mans[n].key+play.mans[n].x+play.mans[n].y
		//}
	}
	var nowTime= new Date().getTime();
	z([h,nowTime-initTime])
	*/
	
}

//判断变将条件


play.isBS = function (bs){
//var isBS=false;
if( bs=='b'){

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
                       
			//if (key=="j0"){ return true;}
                      
			
                    

                         
		}

	}		


}
if( bs=='B'){

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
                       
			//if (key=="J0"){ return true;}
                      
			
                    

                         
		}

	}		


}
if( bs=='s'){

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
                       
			//if (key=="j0"){ return true;}
                      
			if (key=="b0"){ return true;}
                       
                      if (key=="b1"){ return true;}
                          
			if (key==["b2"]){ return true;}
                       
                      if (key==["b3"]){ return true;}
                     
			if (key==["b4"]){ return true;}
                    

                         
		}

	}		


}

if( bs=='S'){

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
                       
			//if (key=="J0"){ return true;}
			if (key=="B0"){ return true;}
                     if (key=="B1"){ return true;}
			if (key==["B2"]){ return true;}
                      if (key==["B3"]){ return true;}
			if (key==["B4"]){ return true;}

                         
		}

	}		


}
if( bs=='x') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		 // if (key==["j0"] ){ return true;}	
                    if (key==["b0"]){ return true;}
                    if (key==["b1"]){ return true;}
			if (key==["b2"]){ return true;}
                     if (key==["b3"]){ return true;}
			if (key==["b4"]){ return true;}
			if (key==["s0"]){ return true;}
                      if (key==["s1"]){ return true;}
			
                         
		}
	}
}
if( bs=='X') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		 // if (key==["J0"] ){ return true;}	
                    if (key==["B0"]){ return true;}
                    if (key==["B1"]){ return true;}
			if (key==["B2"]){ return true;}
                     if (key==["B3"]){ return true;}
			if (key==["B4"]){ return true;}
			if (key==["S0"]){ return true;}
                      if (key==["S1"]){ return true;}
			
                         
		}
	}	
}

if( bs=='m') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		  //if (key==["j0"] ){ return true;}	
                    if (key==["b0"]){ return true;}
                    if (key==["b1"]){ return true;}
			if (key==["b2"]){ return true;}
                     if (key==["b3"]){ return true;}
			if (key==["b4"]){ return true;}
			if (key==["s0"]){ return true;}
                      if (key==["s1"]){ return true;}
			if (key==["x0"]){ return true;}
                      if (key==["x1"]){ return true;}			
                         
		}
	}
}
if( bs=='M') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		 // if (key==["J0"] ){ return true;}	
                    if (key==["B0"]){ return true;}
                    if (key==["B1"]){ return true;}
			if (key==["B2"]){ return true;}
                     if (key==["B3"]){ return true;}
			if (key==["B4"]){ return true;}
			if (key==["S0"]){ return true;}
                      if (key==["S1"]){ return true;}
		   if (key==["X0"]){ return true;}
                      if (key==["X1"]){ return true;}
                         
		}
	}	
}
if( bs=='p') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		// if (key==["j0"] ){ return true;}	
                    if (key==["b0"]){ return true;}
                    if (key==["b1"]){ return true;}
			if (key==["b2"]){ return true;}
                     if (key==["b3"]){ return true;}
			if (key==["b4"]){ return true;}
			if (key==["s0"]){ return true;}
                      if (key==["s1"]){ return true;}
			if (key==["x0"]){ return true;}
                      if (key==["x1"]){ return true;}			
                      if (key==["m0"]){ return true;}
                      if (key==["m1"]){ return true;}			
                         
		}
	}
}
if( bs=='P') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		 // if (key==["J0"] ){ return true;}	
                    if (key==["B0"]){ return true;}
                    if (key==["B1"]){ return true;}
			if (key==["B2"]){ return true;}
                     if (key==["B3"]){ return true;}
			if (key==["B4"]){ return true;}
			if (key==["S0"]){ return true;}
                      if (key==["S1"]){ return true;}
		   if (key==["X0"]){ return true;}
                      if (key==["X1"]){ return true;}
                     if (key==["M0"]){ return true;}
                      if (key==["M1"]){ return true;}			
                        
		}
	}	
}
if( bs=='c') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		// if (key==["j0"] ){ return true;}	
                    if (key==["b0"]){ return true;}
                    if (key==["b1"]){ return true;}
			if (key==["b2"]){ return true;}
                     if (key==["b3"]){ return true;}
			if (key==["b4"]){ return true;}
			if (key==["s0"]){ return true;}
                      if (key==["s1"]){ return true;}
			if (key==["x0"]){ return true;}
                      if (key==["x1"]){ return true;}			
                      if (key==["m0"]){ return true;}
                      if (key==["m1"]){ return true;}
	               if (key==["p0"]){ return true;}
                      if (key==["p1"]){ return true;}			
                         
		}
	}
}
if( bs=='C') {

for (var i=0; i<play.map.length; i++){
		for (var n=0; n<play.map[i].length; n++){
			var key = play.map[i][n];
		// if (key==["J0"] ){ return true;}	
                    if (key==["B0"]){ return true;}
                    if (key==["B1"]){ return true;}
			if (key==["B2"]){ return true;}
                     if (key==["B3"]){ return true;}
			if (key==["B4"]){ return true;}
			if (key==["S0"]){ return true;}
                      if (key==["S1"]){ return true;}
		   if (key==["X0"]){ return true;}
                      if (key==["X1"]){ return true;}
                     if (key==["M0"]){ return true;}
                      if (key==["M1"]){ return true;}			
                       if (key==["P0"]){ return true;}
                      if (key==["P1"]){ return true;}			
                        
		}
	}	
}


return false;

}





//点击棋盘事件
play.clickCanvas = function (e){
         if (!play.isPlay) return false;
        
	var key = play.getClickMan(e);
	var point = play.getClickPoint(e);
	
	var x = point.x;
	var y = point.y;
	
	if (key ){
		play.clickMan(key,x,y);	
	}else {
		play.clickPoint(x,y);	
	}
	play.isFoul = play.checkFoul();//检测是不是长将
}

//点击棋子，四种情况，选中、取消选中、吃子、升变
play.clickMan = function (key,x,y){
	var man = com.mans[key];
        
         // 选中棋子

         if (!play.nowManKey){

          
			//if (com.mans[play.nowManKey]) com.mans[play.nowManKey].alpha = 1 ;
			man.alpha = 0.7;
			com.pane.isShow = false;
			play.nowManKey = key;
			com.mans[key].ps = com.mans[key].bl(); //获得所有能着点
                      
			com.dot.dots = com.mans[key].ps
			com.show();
			//com.get("selectAudio").start(0);
			//com.get("clickAudio").play();
	}
        //取消选中
         else if ( play.nowManKey && !play.indexOfPs(com.mans[play.nowManKey].ps,[x,y])){    
         com.mans[play.nowManKey].alpha = 1 ;
         play.nowManKey =false;       
         com.mans[key].ps =[];
          com.dot.dots =[];

         com.show();
        
        }
         

       //吃子
       //man为被吃掉的棋子
	else if (play.indexOfPs(com.mans[play.nowManKey].ps,[x,y])&& play.nowManKey&&play.nowManKey != key && man.my != com.mans[play.nowManKey ].my){
			man.isShow = false;
                      
			var pace=com.mans[play.nowManKey].x+""+com.mans[play.nowManKey].y
		
         
               com.get("moveInfo").innerHTML='<h3>棋谱记录：</h3>'+com.createMove(play.map,com.mans[play.nowManKey].x,com.mans[play.nowManKey].y,x,y);  
			delete play.map[com.mans[play.nowManKey].y][com.mans[play.nowManKey].x];
                       
			play.map[y][x] = play.nowManKey;
                      	   
			com.showPane(com.mans[play.nowManKey].x ,com.mans[play.nowManKey].y,x,y)////显示移动的棋子外框
			com.mans[play.nowManKey].x = x;
			com.mans[play.nowManKey].y = y;
			com.mans[play.nowManKey].alpha = 1
			//com.pane.isShow = false;这个应该注销
	                if (man.key == "j0") play.isj =false;//超强子被吃恢复可变。
			if (man.key == "J0") play.isJ=false;
			play.pace.push(pace+x+y);

			play.nowManKey = false;
  
			com.dot.dots = [];
                     	com.show();
			com.get("clickAudio").play();
                        play.my=-1;
                        
			if (key == "w0") play.showWin (-1);
			if (key == "W0") play.showWin (1);
		
                       if (play.doublePlay == false){
                           setTimeout("play.AIPlay()",500);
                       }else {
                         play.doublePlay();
                        }
			
		}
 //升变条件：提示点是自身的。
        else if (play.nowManKey && play.indexOfPs(com.mans[play.nowManKey].ps,[x,y])){    
                 man.isShow = false;
	               
                        var pace=com.mans[play.nowManKey].x+""+com.mans[play.nowManKey].y;
                   
			 com.get("moveInfo").innerHTML='<h3>棋谱记录：</h3>'+com.createMove(play.map,com.mans[play.nowManKey].x,com.mans[play.nowManKey].y,x,y);
                         
			delete play.map[com.mans[play.nowManKey].y][com.mans[play.nowManKey].x];
                       
		 if( man.my ==1) {
                    play.ykey1=play.nowManKey;//记录原key，悔棋时用，不知道是否有效
                    com.mans[play.nowManKey]=new com.class.Man("j0",x,y);
                 
                 
                 play.isj=true;
   
                   }
                 if( man.my ==-1)  {
                   play.ykey0 = play.nowManKey;//记录原key，悔棋时用，不知道是否有效
                   com.mans[play.nowManKey]=new com.class.Man("J0",x,y);
                 
                 
                    play.isJ = true;
                   }
 
                 
           
                   
                  
			 

                        play.map[y][x] = play.nowManKey;
                      if (key == "w0") play.showWin1 (-1);
			if (key == "W0") play.showWin1 (1);
 
                     // man.isShow = true;
                       com.get("clickAudio").play();
                      
			com.showPane(com.mans[play.nowManKey].x ,com.mans[play.nowManKey].y,x,y)////显示移动的棋子外框
			com.mans[play.nowManKey].x = x;
			com.mans[play.nowManKey].y = y;
			com.mans[play.nowManKey].alpha = 1
                  
			com.pane.isShow = true;
                          
			 play.pace.push(pace+x+y);
                        //play.nowManKey = false;
			 com.dot.dots = [];
	
	            com.childList.push(com.mans[play.nowManKey]);
                    com.childList.push(com.dot);//被吃时显示可着点。
	                com.show();
			
			 
			
                           play.my=-1;
                          play.isPlay = false;
			if (play.doublePlay==false){
                           setTimeout("play.AIPlay()",500);
                        }else {
                         play.doublePlay();
                       }
                       
     
		}
	
 }
  


//点击着点
play.clickPoint = function (x,y){
	var key=play.nowManKey;
	var man=com.mans[key];
	if (play.nowManKey){
		if (play.indexOfPs(com.mans[key].ps,[x,y])){
			var pace=man.x+""+man.y
			com.get("moveInfo").innerHTML='<h3>棋谱记录：</h3>'+com.createMove(play.map,com.mans[play.nowManKey].x,com.mans[play.nowManKey].y,x,y);
			delete play.map[man.y][man.x];
			play.map[y][x] = key;
			com.showPane(man.x ,man.y,x,y)
			man.x = x;
			man.y = y;
			man.alpha = 1;
			play.pace.push(pace+x+y);
			play.nowManKey = false;
			com.dot.dots = [];
			com.show();
			com.get("clickAudio").play();
                           play.my=-1;
                          play.isPlay = false;
			if (play.doublePlay == false){
                           setTimeout("play.AIPlay()",500);
                        }else {
                         play.doublePlay();
                       }
		}else{
			//alert("不能这么走哦！")	
		}
	}
	
}


//悔棋
play.regret = function (){

	var map  = com.arr2Clone(com.initMap);
	//初始化所有棋子
	for (var i=0; i<map.length; i++){
		for (var n=0; n<map[i].length; n++){
			var key = map[i][n];
			if (key){
				com.mans[key].x=n;
				com.mans[key].y=i;
				com.mans[key].isShow = true;
			}
		}
	}
	var pace= play.pace;
	pace.pop();
	//pace.pop();//加上按回合退，不妥。
	
	for (var i=0; i<pace.length; i++){
		var p= pace[i].split("")
		var x = parseInt(p[0], 10);
		var y = parseInt(p[1], 10);
		var newX = parseInt(p[2], 10);
		var newY = parseInt(p[3], 10);
                var key=map[y][x];

            
             
		//try{
	 
		var cMan=map[newY][newX];

            
       


		if (cMan) com.mans[map[newY][newX]].isShow = false ;
		com.mans[key].x = newX;
		com.mans[key].y = newY;
		map[newY][newX] = key;               
                
		
             


		if (i==pace.length-1){
			com.showPane(newX ,newY,x,y)
                        	
		}
		//} catch (e){
		//	com.show()
		//	z([key,p,pace,map])
			
		//	}
	}

 
	play.map = map;
	play.my=1;
	play.isPlay=true;

	com.show();

}
//增加双人模式
play.doublePlay =function (){

play.doublePlay==true;
  if( play.my==-1){
        play.my=1;
        play.isPlay=true;
    }else{
        play.my=-1;
        play.isPlay=true;
    }
}

//Ai自动走棋
play.AIPlay = function (){
	//return
	
     
        play.my=-1;
 
	var pace=AI.init(play.pace.join(""))
	if (!pace) {
		play.showWin (1);
		return ;
	}
	play.pace.push(pace.join(""));
	var key=play.map[pace[1]][pace[0]]
		play.nowManKey = key;
	
	var key=play.map[pace[3]][pace[2]];
	if (key){
		play.AIclickMan(key,pace[2],pace[3]);	
	}else {
		play.AIclickPoint(pace[2],pace[3]);	
	}
	com.get("clickAudio").play();
	if (play.my==-1)
      play.isPlay=true;
  com.get("moveInfo").innerHTML='<h3>棋谱记录：</h3>'+com.createMove(play.map,man.x,man.y,x,y);   
}


//检查是否长将？
play.checkFoul = function(){
	var p=play.pace;
	var len=parseInt(p.length,10);
	if (len>11&&p[len-1] == p[len-5] &&p[len-5] == p[len-9]){
		return p[len-4].split("");
	}
	return false;
}



play.AIclickMan = function (key,x,y){
	var man = com.mans[key];
	//吃子
	man.isShow = false;
	delete play.map[com.mans[play.nowManKey].y][com.mans[play.nowManKey].x];
	play.map[y][x] = play.nowManKey;
	play.showPane(com.mans[play.nowManKey].x ,com.mans[play.nowManKey].y,x,y)
	
	com.mans[play.nowManKey].x = x;
	com.mans[play.nowManKey].y = y;
	play.nowManKey = false;
	
	com.show()
	if (key == "w0") play.showWin (-1);
	if (key == "W0") play.showWin (1);
         
	
}

play.AIclickPoint = function (x,y){
	var key=play.nowManKey;
	var man=com.mans[key];
	if (play.nowManKey){
		delete play.map[com.mans[play.nowManKey].y][com.mans[play.nowManKey].x];
		play.map[y][x] = key;
		
		com.showPane(man.x,man.y,x,y)
		
	
		man.x = x;
		man.y = y;
		play.nowManKey = false;
		
	}
	com.show();
}

//？功能是判断是否在可着点，但看不明白为什么。3948
play.indexOfPs = function (ps,xy){
	for (var i=0; i<ps.length; i++){
		if (ps[i][0]==xy[0]&&ps[i][1]==xy[1]) return true;
	}
	return false;
	
}

//获得点击的着点
play.getClickPoint = function (e){
	var domXY = com.getDomXY(com.canvas);
	var x=Math.round((e.pageX-domXY.x-com.pointStartX-20)/com.spaceX)
	var y=Math.round((e.pageY-domXY.y-com.pointStartY-20)/com.spaceY)
	return {"x":x,"y":y}
}

//获得棋子
play.getClickMan = function (e){
	var clickXY=play.getClickPoint(e);
	var x=clickXY.x;
	var y=clickXY.y;
	if (x < 0 || x>8 || y < 0 || y > 9) return false;
	return (play.map[y][x] && play.map[y][x]!="0") ? play.map[y][x] : false;
}

play.showWin = function (my){
	play.isPlay = false;
	if (my===1){
		alert("红方赢了！");
	}else{
		alert("黑方赢了！");
	}
}

play.showWin1 = function (my){
	play.isPlay = false;
	if (my===1){
		alert("黑方认输了！");
	}else{
		alert("红方认输了！");
	}
}


